package com.t.bricks.bussiness.db.dao.sequence.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.t.bricks.bussiness.db.dao.sequence.SequenceDao;
import com.t.bricks.bussiness.db.mapper.sequence.SequenceMapper;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;

@Repository
public class SequenceDaoImpl implements SequenceDao {
	
	@Autowired
	private SequenceMapper sequenceMapper;
	
	/**
	 * 创建序列表
	 * @param dbName 所在库名
	 * @return
	 */
	//@Autowired
	@Override
	public MsgEmity createTable(String dbName) {
		String db = dbName;
		if (dbName.endsWith(".")) {
			db = db.substring(0, db.length() -1);
		}
		
		try {
			int i = sequenceMapper.checkTable(db);//判断序列表是否存在
			if (i > 0) {
				return new MsgEmity(true, "序列表已经存在！", 7999);
			}
			
			sequenceMapper.createTable(dbName);//创建表,成功返回的是0,否则会抛异常
		} catch (Exception e) {
			Log.error("创建序列表失败时发生异常:", e.getMessage());
			return new MsgEmity(false, "创建序列表失败！", 7002);
		}
		
		return new MsgEmity(true, "创建序列表成功！", 7999);
	}
	
	/**
	 * 判断序列表是否存在
	 * @param dbName 所在库名
	 * @return
	 */
	@Override
	public MsgEmity checkTable(String dbName) {
		try {
			int i = sequenceMapper.checkTable(dbName);
			if (i < 1) {
				return new MsgEmity(true, "序列表不存在！", 0);
			}
		} catch (Exception e) {
			Log.error("判断序列表是否存在时发生异常:", e.getMessage());
			return new MsgEmity(false, "判断序列表是否存在失败！", 7002);
		}
		
		return new MsgEmity(true, "序列表不存在！", 1);
	}

	/**
	 * 取各表的新Id
	 * @param dbName 所在库名
	 * @param formatLong 格式化长度(不足长度+0)
	 * @param sName
	 * @return
	 */
	@Override
	public synchronized String getNewId(String dbName, int formatLong, String sName) {
		try {
			int iCount = sequenceMapper.newValue(dbName, sName);//更新到新iValue值,即值加1
			if (iCount != 1) {
				iCount = sequenceMapper.addSequence(dbName, sName, 1);
				if (iCount != 0) {
					StringBuilder sb = new StringBuilder(6).append("%0").append(formatLong).append("d");
					return String.format(sb.toString(), 1);
				}
				
				return "0";//说明是取值失败
			}
		} catch (Exception e) {
			Log.error("添加序列发生异常:", e.getMessage());
			return "0";
		}
		
		StringBuilder sb = new StringBuilder(6).append("%0").append(formatLong).append("d");
		try {
			int iValue = sequenceMapper.findLastId(dbName, sName);//取最后的值
			return String.format(sb.toString(), iValue);
		} catch (Exception e) {
			Log.error("取新编号发生异常:", e.getMessage());
			return "0";
		}
	}
	
	/**
	 * 取各表的一批新Id
	 * @param dbName 所在库名
	 * @param formatLong 格式化长度(不足长度+0)
	 * @param sName
	 * @param size 数量
	 * @return
	 */
	@Override
	public synchronized List<String> getNewIds(String dbName, int formatLong,
			String sName, int size) {
		List<String> result = new ArrayList<String>(size);
		
		int iCount = sequenceMapper.newValues(dbName, sName, size);
		if (iCount != 1) {
			iCount = sequenceMapper.addSequence(dbName, sName, size);
		}
		
		if (iCount != 1) {
			result.add("0");
			return result;//说明是取值失败
		}
		
		StringBuilder sb = new StringBuilder(6).append("%0").append(formatLong).append("d");
		
		int vId = sequenceMapper.findLastId(dbName, sName);//取得最大的id,因使用代码锁,可保证不会出现冲突
		for (int i = size -1; i > 0; i--) {
			result.add(String.format(sb.toString(), (vId - i)));
		}
		
		return result;
	}
	
	/**
	 * 重置
	 * @param dbName 所在库名
	 * @param sName 序列名
	 * @param iInitial 初始值
	 * @return
	 */
	@Override
	public int reset(String dbName, String sName, Integer iInitial) {
		int iCount = sequenceMapper.reset(dbName, sName, iInitial);
		
		if (iCount < 1) {
			return 0;
		}
		
		return 1;
	}
	
}
